package cn.cocho.android.mc.comp.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import cn.cocho.android.mc.R;


public class McTextView extends TextView {

	private int textTouchColor;
	private int textColor;

	public McTextView(Context context) {
		super(context);
	}

	public McTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public McTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray tdarr = context.obtainStyledAttributes(attrs, R.styleable.McTextView);
		textTouchColor = tdarr.getColor(R.styleable.McTextView_textView_touchColor, Color.WHITE);
		textColor = getCurrentTextColor();
		tdarr.recycle();
	}

	/**
	 * 判断该次点击操作是否有效
	 * 
	 * @param event
	 * @return true:有效；false:无效
	 * @time 2013-4-28上午10:57:33
	 */
	public static boolean isClick(MotionEvent event, View view) {
		return event.getX() > 0 && event.getY() > 0 && event.getX() < view.getWidth() && event.getY() < view.getHeight();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setTextColor(textTouchColor);
			break;
		case MotionEvent.ACTION_UP:
			setTextColor(textColor);
			if (isClick(event, this)) {
				performClick();// 如果不加该方法，则在为该按钮添加点击事件时，点击事件失效
			}
			break;
		default:
			break;
		}
		return true;
	}
}